package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisVORedisService  strategyStatisVORedisService;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("---------------begin---------------------");

        //  查询数据库中攻略表的所有数据
        List<Strategy> strategyList = strategyService.selectAll();
        //  循环遍历
        for (Strategy strategy : strategyList) {
            //  判断该攻略是否存在缓存中
            if (strategyStatisVORedisService.isStrategyVoExists(strategy.getId()))  {
                //  若存在，则跳出当前循环
                continue;
            }
            //  若不存在，则把数据库中的数据初始化到缓存中
            StrategyStatisVO strategyStatisVO = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy, strategyStatisVO);
            strategyStatisVO.setStrategyId(strategy.getId());
            strategyStatisVORedisService.setStrategyStatisVo(strategyStatisVO);
        }

        System.out.println("---------------end-----------------------");
    }
}
